YZYZ.Admin.Attractions 				= {};
YZYZ.Admin.Attractions.win			= null;
YZYZ.Admin.Attractions.gridPanel 	= null;
YZYZ.Admin.Attractions.store 		= null;

YZYZ.Admin.Attractions.addForm		= null;
YZYZ.Admin.Attractions.addWin		= null;
YZYZ.Admin.Attractions.columnModel 	= null;
YZYZ.Admin.Attractions.newRecord = Ext.data.Record.create([
	{ name: 'id', type: 'int'},
	{ name: 'name', type: 'string'},
	{ name: 'des', type: 'string'},
	{ name: 'ownerId', type: 'int'}
]);
YZYZ.Admin.Attractions.submitNewAtt=function(){
	var name 	= Ext.getCmp("addAttName").getValue();
	var des 	= Ext.getCmp("addAttDes").getValue();
	var ownerId = YZYZ.Admin.Scenic.grid.getSelectionModel().getSelected().get("id");
	if(Ext.isEmpty(name)){
		YZYZ.Curtain.alert("名称不能为空"),
		Ext.getCmp("addAttName").markInvalid();
		return;
	}
	if(name.length > 100){
		YZYZ.Curtain.alert("名称长度不能超过100"),
		Ext.getCmp("addAttName").markInvalid();
		return;
	}
	if(Ext.isEmpty(name)){
		YZYZ.Curtain.alert("描述不能为空"),
		Ext.getCmp("addAttDes").markInvalid();
		return;
	}
	if(des.length > 2000){
		YZYZ.Curtain.alert("描述长度不能超过2000"),
		Ext.getCmp("addAttDes").markInvalid();
		return;
	}
	YZYZ.Curtain.msg("系统处理中....");
	Ext.Ajax.request( {
		url		: Ext.get("addAttUrl").dom.value,
		timeout	: 30000,
		method	: 'POST',
		params	: {
			name 		: name,
			description	: des,
			ownerId 	: ownerId
		},
		success : function(result, request) {
			result = Ext.util.JSON.decode(result.responseText);
			if(result.code == YZYZ.success){
				var rec = new YZYZ.Admin.Scenic.newRecord({
					name 		:  name,
					description : des,
					ownerId		: ownerId,
					id 			: result.id
					
				});
				YZYZ.Curtain.msg("添加成功");
				YZYZ.Admin.Attractions.gridPanel.getStore().insert(0, rec);
				YZYZ.Admin.Attractions.addWin.hide();			
			}
			else {
				YZYZ.Curtain.alert(result.message);
			}
		},
		failure : function(result, request) {
			YZYZ.Curtain.alert("操作失败！程序错误或网络故障");
		}
	});	
	
};
YZYZ.Admin.Attractions.showNewWin=function(){
	if(!YZYZ.Admin.Attractions.addWin){
		YZYZ.Admin.Attractions.addForm=new Ext.form.FormPanel({
			width:600,
			height:400,
			region:'center',
			labelWidth:80,
			labelAlign:'right',
			bodyStyle:'padding:10px;',
			items:[{
				xtype:'textfield',
				width:400,
				fieldLabel:'名称',
				id:'addAttName',
				maxLength:100
			},{
				xtype:'htmleditor',
				width:500,
				height:250,
				fieldLabel:'描述',
				id:'addAttDes',
				maxLength:2000
			}]
		});
		YZYZ.Admin.Attractions.addWin=new Ext.Window({
			layout:'border',
			title:'添加景点',
			closable:true,
			closeAction:'hide',
			modal:true,
			width:650,
			height:400,
			closeAction:'hide',
			items:[YZYZ.Admin.Attractions.addForm],
			buttons:[{
				text:'提交',
				width:60,
				handler:YZYZ.Admin.Attractions.submitNewAtt
			},{
				text:'关闭',
				width:60,
				handler:function(){YZYZ.Admin.Attractions.addWin.hide()}
			}]
		})
	}
	YZYZ.Admin.Attractions.addWin.show();
};

YZYZ.Admin.Attractions.confirm=function(id){
	Ext.MessageBox.show({
		title:'确认',
		msg:'您确定要删除此景点吗？',
		buttons:Ext.MessageBox.OKCANCEL,
		fn:function(btn){YZYZ.Admin.Attractions.handleDelete(btn,id);}
	});
};
YZYZ.Admin.Attractions.handleDelete=function(btn,id){
	if(btn!='ok'){
		return;
	}
	YZYZ.Curtain.msg("系统处理中....");
	Ext.Ajax.request( {
		url		: Ext.get("deleteAttUrl").dom.value,
		timeout	: 30000,
		method	: 'POST',
		params	: {
			attId:id
		},
		success : function(result, request) {
			result = Ext.util.JSON.decode(result.responseText);
			if(result.code == YZYZ.success){
				YZYZ.Curtain.msg("删除成功");
				YZYZ.Admin.Attractions.gridPanel.getStore().remove(YZYZ.Admin.Attractions.gridPanel.getSelectionModel().getSelected());	
			}
			else {
				YZYZ.Curtain.alert(result.message);
			}
		},
		failure : function(result, request) {
			YZYZ.Curtain.alert("操作失败！程序错误或网络故障");
		}
	});	
};
YZYZ.Admin.Attractions.opRender=function(value, metaData, record, rowIndex, colIndex, store){
	var html = "<div class='btnCotainer'>"
	html = html + "<div class='divBtn' ><button type='button' onclick='YZYZ.Admin.Attractions.showEditWin("+record.get('id')+")' class='btnEdit'>编辑</button></div>";
	html = html + "<div class='divBtn'><button type='button' class='btnEdit' onclick='YZYZ.Admin.Attractions.confirm("+record.get('id')+")'>删除</button></div>";
	html = html + "</div>"
	return html;
};
YZYZ.Admin.Attractions.showAttPanel=function(id){
	if(!YZYZ.Admin.Attractions.win){
		YZYZ.Admin.Attractions.columnModel = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(),
            {header: '名称', width: 250, sortable: false, hideable:false, dataIndex: 'name'},
            {header: '操作',id:'attOperations', width: 300, sortable: false, hideable:false, dataIndex: 'ownerId', renderer:YZYZ.Admin.Attractions.opRender}
    	]);
    	YZYZ.Admin.Attractions.store= new Ext.data.Store({  
			proxy: new Ext.data.HttpProxy ({
	            url: document.getElementById("getAttsUrl").value
	        }),
	        reader: new Ext.data.JsonReader({
	        	root			: "data",
	        	totalProperty	: "sum"
        	}, [
                {name:'id'},
                {name:'name'},
                {name:'description'},
                {name:'ownerId', type:'int'}
            ]),
	        remoteSort: false
		});
		YZYZ.Admin.Attractions.gridPanel=new Ext.grid.GridPanel({
			id			: "attsGrid",
			store		: YZYZ.Admin.Attractions.store,
			cm			: YZYZ.Admin.Attractions.columnModel,
			sm			: new Ext.grid.RowSelectionModel(),
			region		: 'center',
			height		: 400,
			width		: 700,
			autoScroll  : true,
			viewConfig	: { forceFit:true },
			autoExpandColumn : 'attOperations',
			loadMask	: { msg:"数据加载中....." },
			bbar		: new Ext.PagingToolbar({   
				pageSize	: 30,   
				store		: YZYZ.Admin.Attractions.store,   
				displayInfo	: true,   
				displayMsg	: "第{0}条到{1}条  共{2}条",   
				emptyMsg	: "没有景点",
				items		:[{text:'添加景点',handler:YZYZ.Admin.Attractions.showNewWin}]
			})              
		});
		YZYZ.Admin.Attractions.win=new Ext.Window({
			title:'景区景点',
			width:700,
			height:400,
			layout:'border',
			closable:false,
			modal:true,
			items:[YZYZ.Admin.Attractions.gridPanel],
			buttons:[{
				text:'关闭',
				handler:function(){YZYZ.Admin.Attractions.win.hide();}
			}]
		});
	}
	YZYZ.Admin.Attractions.win.show();
	YZYZ.Admin.Attractions.gridPanel.getStore().setBaseParam("scenicId", YZYZ.Admin.Scenic.grid.getSelectionModel().getSelected().get("id"));
	YZYZ.Admin.Attractions.gridPanel.getStore().load({params:{start:0,limit:30}});
	
};